<!-- 商品分类 -->
<template>
  <div class="flexbox">
    <h2 style="color: #595959"> {{ $t("messages.ProductCategory") }}</h2>
    <el-button
      style="
        border: 1px solid #409eff;
        background-color: #fff;
        color: #409eff;
        width: 80px;
        height: 30px;
      "
      type="text"
      size="small"
      @click="handleAdd()"
      >添加分类</el-button
    >
  </div>
  <div>
    <!-- .表格 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="序号" type="index" width="80"></el-table-column>
      <el-table-column prop="cateName" label="分类名称">
        <template #default="scope">
          <div v-if="scope.row === editingRow">
            <el-input v-model="scope.row.cateName"></el-input>
          </div>
          <div v-else>
            {{ scope.row.cateName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="state" label="是否启用">
        <template #default="scope">
          <el-switch
            :disabled="scope.row === editingRow ? false : true"
            v-model="scope.row.state"
            @change="handleSwitchChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            v-if="scope.row === editingRow"
            style="
              background-color: #68d74c;
              color: #fff;
              width: 60px;
              height: 30px;
            "
            type="text"
            size="small"
            @click="handleComplete(scope.row)"
            >完成</el-button
          >
          <el-button
            v-else
            style="
              background-color: #409eff;
              color: #fff;
              width: 60px;
              height: 30px;
            "
            type="text"
            size="small"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            style="
              border: 1px solid #e8e8e8;
              background-color: #e55443;
              color: #fff;
              width: 60px;
              height: 30px;
            "
            type="text"
            size="small"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- .分页器 -->
    <el-pagination
      style="margin-top: 10px"
      background
      layout="total, sizes ,prev, pager, next"
      :total="total"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    >
      >
    </el-pagination>
    <!-- .对话框 -->
    <el-dialog
      center
      lock-scroll
      draggable
      title="添加分类"
      v-model="dialogVisible"
      width="46%"
      @close="dialogVisible = false"
    >
      <el-form :model="form">
        <el-form-item
          label="分类名称"
          style="margin-left: 20px; margin-top: 20px"
        >
          <el-input v-model="form.cateName" style="width: 95%"></el-input>
        </el-form-item>
        <el-form-item
          label="是否启用"
          style="margin-left: 20px; margin-bottom: 20px"
        >
          <el-switch v-model="form.state"></el-switch>
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ElMessage, ElMessageBox } from "element-plus";
import { ref, onMounted } from "vue";
import {
  getCategories,
  editCategory,
  deleteCategory,
  getCategoryList,
  addCategory,
} from "@/api/index.js";
const editingRow = ref(null); //存储当前行
const total = ref(11); // 总数据量
const pageSize = ref(7); // 每页显示的数据量(search)
const currentPage = ref(1); // 当前页码(search)
const dialogVisible = ref(false); // 对话框显示状态
const form = ref({
  cateName: "",
  state: true,
});

// 表格数据
const tableData = ref([
  { id: 1, cateName: "分类1", state: 1 },
  { id: 2, cateName: "分类2", state: 1 },
  // ... 更多分类数据
]);

// 获取分类列表
const getCategoryListFn = async () => {
  const res = await getCategoryList({
    currentPage: currentPage.value,
    pageSize: pageSize.value,
  });
  res.data.forEach((item) => {
    item.state = item.state === 1 ? true : false;
  });
  tableData.value = res.data;
  total.value = res.total;
};

// 添加按钮
const handleAdd = () => {
  dialogVisible.value = true;
};

// 提交表单
const submitForm = async () => {
  const res = await addCategory(form.value);
  if (res.code === 0) {
    ElMessage.success("添加成功");
    form.value.cateName = "";
    form.value.state = true;
    dialogVisible.value = false;
    getCategoryListFn();
  } else {
    ElMessage.error("添加失败");
  }
};

// 编辑操作
const handleEdit = (row) => {
  editingRow.value = row;
};

// 编辑完成
const handleComplete = async (row) => {
  try {
    const result = await editCategory({
      id: row.id,
      cateName: row.cateName,
      state: row.state,
    });

    if (result.code === 0) {
      ElMessage.success("更新成功");
      editingRow.value = null; // 重置编辑行
    } else {
      ElMessage.error("更新失败");
    }
  } catch (error) {
    console.error("更新失败:", error);
    ElMessage.error("更新异常");
  }
};

// 删除操作
const handleDelete = async (row) => {
  ElMessageBox.confirm("确定要删除本分类吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const res = await deleteCategory({
        id: row.id,
      });
      if (res.code === 0) {
        ElMessage({
          type: "success",
          message: "删除成功",
        });
        getCategoryListFn();
      }
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};

// 开关状态改变时的处理函数
const handleSwitchChange = (row) => {
  // row.state
};

// .分页器
const handleCurrentChange = (val) => {
  currentPage.value = val;
  getCategoryListFn();
};

// 每页条目数改变时的处理函数
const handleSizeChange = (newSize) => {
  pageSize.value = newSize;
  currentPage.value = 1; // 重置为第一页
  getCategoryListFn();
};

onMounted(() => {
  getCategoryListFn();
});
</script>

<style scoped>
.flexbox {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>
